<?php
include "connection.php";
 
    if(isset($_POST['khuvuc']) && isset($_POST['hocky'])) 
    {
        $arr = explode('.',$_POST['hocky']);
        $hocky = $arr[0];
        $nam = $arr[1];
        
        $khoa = $lop = '';
        switch($_POST['khuvuc']){
            case "khoa": $khoa = $_POST['khoa']; 
                         $lop = '';
                         break;
            case "lop": $arr = explode('.',$_POST['lop']);  
                        $khoa = $arr[0];
                        $lop = $arr[1];
                        break;
            default: $khoa = ''; $lop = '';
        }
        
        $sql = mysql_query("SELECT ketqua.MSSV, HOTEN, sinhvien.MAKHOA, KHOAHOC, ketqua.HOCKY, NAM, 
        TRUNCATE(SUM( DIEMTRUNGBINH * TINCHILYTHUYET ) / SUM( TINCHILYTHUYET ),2) AS TRUNGBINHHOCKY
        FROM  `ketqua` , monhoc, sinhvien WHERE ketqua.MAMONHOC = monhoc.MAMONHOC AND ketqua.MSSV = sinhvien.MSSV
        AND sinhvien.MAKHOA LIKE '$khoa%' AND KHOAHOC LIKE '$lop%' AND ketqua.HOCKY = '$hocky' AND NAM = '$nam'
        GROUP BY sinhvien.MSSV ORDER BY TRUNGBINHHOCKY DESC");        
                
?>
        
            <tr>
                <td>MSSV</td>
                <td>Họ tên</td>
                <td>Lớp</td>
                <td>ĐTB</td>
            </tr>
        <?php
        while($rows = mysql_fetch_assoc($sql)) {
            echo "<tr>";
            echo "<td>".$rows['MSSV']."</td>";   
            echo "<td>".$rows['HOTEN']."</td>";
            echo "<td>".$rows['MAKHOA'].".".$rows['KHOAHOC']."</td>";
            echo "<td>".$rows['TRUNGBINHHOCKY']."</td>";
            echo "</tr>";       
        }            
        ?>
<?php
        die;
    }    
?>